﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.IO;

namespace crm
{
    public partial class frmCRMmain : Form
    {
        public frmCRMmain()
        {
            InitializeComponent();
        }

        private void btnZaloguj_Click(object sender, EventArgs e)
        {
            MySqlConnection mysqlConn = new MySqlConnection(Global.connect);
            mysqlConn.Open();

            string strSQL = "SELECT 1 from " + crm.Properties.Settings.Default.UsersTable + " where user = '"+txtUzytkownik.Text+"' and pass = '"+txtHaslo.Text+"'";
            MySqlCommand mysqlCmd = new MySqlCommand(strSQL, mysqlConn);
            
            int thereIs = Convert.ToInt32(mysqlCmd.ExecuteScalar());

            mysqlConn.Close();

            if (thereIs == 1)
            {
                string filePath = @"C:\test.csv";  
	            string delimiter = ",";  
	 
	            string[][] output = new string[][]{  
	                new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"},  
	                new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"}  
	            };  
	            int length = output.GetLength(0);  
	            StringBuilder sb = new StringBuilder();  
	            for (int index = 0; index < length; index++)  
	                sb.AppendLine(string.Join(delimiter, output[index]));  
	 
	            File.WriteAllText(filePath, sb.ToString());

                Global.exit();
            }
            else
            {
                txtHaslo.ForeColor = System.Drawing.Color.Red;
                txtUzytkownik.ForeColor = System.Drawing.Color.Red;
                lbBlad.Visible = true;
            }
        }

        private void frmCRMmain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Global.exit();
        }

        private void txtUzytkownik_Enter(object sender, EventArgs e)
        {
            txtUzytkownik.ForeColor =  Color.FromKnownColor(System.Drawing.KnownColor.WindowText);
        }

        private void txtHaslo_Enter(object sender, EventArgs e)
        {
            txtHaslo.ForeColor = Color.FromKnownColor(System.Drawing.KnownColor.WindowText);
        }
    }
}
